<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('销售合同新增及修改')" />
	<th:block th:include="include :: businessCSS" />
	<style type="text/css">
	.layui-layer-content{
		margin:0;
	}
	</style>
</head>
<body class="no-skin">
<div class="main-container" id="main-container">
	<div class="main-content">
		<form class="layui-form head-form">
		    <div class="north">
		       	<th:block th:include="include :: business_btn" />
		        <th:block th:include="include :: business_tpl('top')" />
	    	</div>
		    <div class="mid-content">
		        <table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
		    </div>
		   	<th:block th:include="include :: pageFooter" /> 
        </form>
	</div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: businessJS" />
<th:block th:include="include :: businessTableBtn" />
<!-- 合同评审 -->
<script id="change1" type="text/template">
	<table class="layui-hide" id="dhtable_c" lay-filter="ftable_c"></table>
</script>
<th:block th:include="include :: inlineJS" />
<script type="text/javascript">
var $,layer,form,table,laydate,element;//定义layui模块
eachFields();//明细表格中的字段遍历
if(!!$("#fXsFpsl_select").val())$("#fXsFpsl").val($("#fXsFpsl_select").val());//默认税率赋值至input
layui.use(['laydate', 'table','form','layer','jquery','util','element'], function () {
 	$ = layui.$,layer = layui.layer,form = layui.form,table = layui.table,laydate = layui.laydate,util = layui.util,element=layui.element;
 	beforeInit();//表单初始化之前的动作集合
 	layui.element.render();
 	$.get("/business/bargainin/listbynid?nid="+nid,function(data){
		var sumBPrice=0,sumFPsCb=0;
		$.each(data.data,function(k,v){
			dataStr.push(v);
			sumBPrice+=(v.bPrice*(v.bNumb-v.fThSl));
			sumFPsCb+=(v.fPsCbHs*(v.bNumb-v.fThSl));
		});
 		$('#fZdPsMll').val((sumBPrice && sumFPsCb)?(((sumBPrice-sumFPsCb)/sumBPrice * 100).toFixed(decimalObj.fPsMll)+ "%"):'');//整单评审毛利率计算赋值
		initTable();//初始化表格
	});
	//头部event事件
	table.on('toolbar(ftable)', function(obj){
		var checkStatus = table.checkStatus('dhtable');
	    switch(obj.event){
	    	case 'addBtn':
	    		if(listData.length > 0){
	    			if(listData[listData.length - 1]['iid']){
	    				gridColumns.gridNewLine.iid = Number(listData[listData.length - 1]['iid']) + 1;//行号自增1
	    			}else{
	    				gridColumns.gridNewLine.iid = 1;
	    			}
	    		}else{
	    			gridColumns.gridNewLine.iid = 1;
	    		}
	    		listData.push(gridColumns.gridNewLine);
	        	table.reload('dhtable', {data : listData});
	      		form.render();
	    	break;
	    	case 'deleteBatch':
	    		tableDeleteBatch('dhtable', listData, checkStatus);
	    	break;
	    	case 'fieldSet':
	    		filedSet(moduleTable+'_EditPage202109');
         	break;
			case 'tableFull'://表格展开
				$('.north .layui-form').hide();
				table.reload('dhtable',{
					defaultToolbar: [{title:'收缩',layEvent:'tableShrink',icon:'layui-icon-screen-restore'}],
					height: $(window).height()-$('.north').height()-60<300?'300':$(window).height()-( $('.north').height()+$('.south').height()+12)
				});
			break;
			case 'tableShrink'://表格收缩
				$('.north .layui-form').show();
				table.reload('dhtable',{
					defaultToolbar: [{title:'展开',layEvent:'tableFull',icon:'layui-icon-screen-full'}],
					height: $(window).height()-$('.north').height()-60<300?'300':$(window).height()-( $('.north').height()+$('.south').height()+12)
				});
			break;
	    	case 'importBtn':
	    		$.tablelist.importExcel('dhtable',importFields, table.cache.dhtable, moduleTable, decimalObj);
         	break;
	    	case 'model'://导入型号
	    		layer.open({
	    			type: 1, 
	    			offset: '20%',
	    			title: '导入型号（每个型号以回车换行区分）',
            		shade: 0.3,//遮罩
            		shadeClose: true,// 弹层外区域关闭
	    			area: ['400px', '350px'],
	    			content: '<textarea id="textareaVal" style="width:398px;height:240px;background:#e2eff9"></textarea>',
	    			btn: ['确定', '取消'],
	    			yes: function(index, layero){
	    				var textareaVal = $("#textareaVal").val();
	    	    		$.each(textareaVal.split('\n'),function(i,v){
		    				var newData={};//不能直接使用gridColumns.gridNewLine,会破坏原始新增行模板,并且需要重新new
		    				$.each(gridColumns.gridNewLine,function(k,j){newData[k]=j;});//基础新增行模板
		    				if(!!listData.length)newData['iid']=Number(listData[listData.length-1]['iid'])+1;else newData['iid']=1;//iid赋值
	    	    			newData['gID']=v;
	    	    			listData.push(newData);
	    	    		})
	    	    		table.reload('dhtable', {data : listData});
	    				layer.close(index);
	    			}
	    		});
    		break;
	    	case 'matching'://匹配产品
				var postData=listData,relationguid;
				$.each($('.layui-nav dl dd a'),function(i,v){
					if($(v).text().indexOf("产品信息")>-1)relationguid=$(v).attr('value');
				})
				$.each(postData,function(i,v){v['matchIid']=v['iid'];})
	    		$.post('/business/product/matchlist', {'relationguid':relationguid,'productInfoItems':JSON.stringify(postData)}, function(res){
    				if(res.productlist.code==0){
    					if(res.productlist.count==0){
    						layer.msg('无匹配项！',{icon:7});
    					}else{
    	    				$.each(res.productlist.data,function(j,k){
    		    				$.each(listData,function(i,v){
    		    					if(k.matchIid==v.iid)$.each(res.foldRelation.gridFields,function(k2,v2){if(k[v2])v[k2]=k[v2];});//防止赋空值
    		    				})
    	    				})
							listData = calcDetail(listData);
    	    				layer.msg(res.productlist.msg,{icon:1});
    	    				table.reload('dhtable', {data : listData});
    					}
	    			}else{
	    				layer.msg(res.productlist.msg,{icon:2});
	    			}
    		    });
    		break;
	    	case 'confirmation'://合同确认（商务员）
	    		var qrFlag=$('#vQrZt').val()=='未确认'?true:false;
	    		$.modal.confirmCom(qrFlag?'是否确认该销售合同？':'是否取消确认该销售合同？',function(result){
	    	    	$.each(table.cache.dhtable,function(i,v){
	    	    		v['vQrZt']=(qrFlag?'已确认':'未确认');
	    	    	})
	    	    	$.operate.post(moduleUrl+'/qrpost', {'infoItems':JSON.stringify(table.cache.dhtable)},function(res){
	    	    		if(res.code==0){
							setTimeout(function(){
					            window.location.reload();
			                }, 1000);
			        	}
	    	    	});
	    			layer.closeAll();
	    	    });
	    	break;
	    };
	});
	toolEvent('ftable');//行event事件监听
	monitorSort();//监听排序——重载赋值渲染
	moneyCalculation();//表格编辑——资金计算
})
</script>
</body>
</html>